import java.io.BufferedWriter;
import java.io.IOException;
import java.io.OutputStream;
import java.io.OutputStreamWriter;

public class Response {
    private BufferedWriter bw;
    public  int code = 200;  //状态码
    private Session session;
    public Response(OutputStream outputStream,Session session) {
        this.session=session;
        bw = new BufferedWriter(new OutputStreamWriter(outputStream));
    }

    public Session getSession() {
        return session;
    }

    //向客户端发送响应
    public void print(String html) throws IOException {
        if(code==200)
        {
            bw.write("HTTP/1.1 200 OK\n");
            bw.write("Content‐Type:text/html;charset=utf-8\n");//UTF-8
            bw.write("\n");
/*            bw.write("<html lang=\"zh-CN\"><head>\n" +
                    "<meta charset=\"utf-8\"></head>"+html+"</html>");*/
            bw.write(html);
        }else if(code==404)
        {
            bw.write("HTTP/1.1 404 NOT FOUND\n");
            bw.write("Content‐Type:text/html;charset=utf-8\n");//UTF-8
            bw.write("\n");
            bw.write("<html lang=\"zh-CN\"><head>\n" +
                    "<meta charset=\"utf-8\"></head><h1>404 页面找不到</h1></html>");
        }
        else if(code==302)
        {
            bw.write("HTTP/1.1 302\n");
            bw.write("location: file:///C:/\n");
            bw.write("\n");
        }
        else
        {
            bw.write("HTTP/1.1 500 Server ERROR\n");
            bw.write("Content‐Type:text/html;charset=utf-8\n");//UTF-8
            bw.write("\n");
            bw.write("<html lang=\"zh-CN\"><head>\n" +
                    "<meta charset=\"utf-8\"></head><h1>500 服务器错误</h1></html>");
        }
        bw.flush();
    }

    public void respJsp(String path) throws IOException {

    }

    public void setCode(int code) {
        this.code = code;
    }
}
